<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>Threadv</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta property="og:type" content="website">
<meta property="og:title" content="Threadv">
<meta property="og:url" content="http://yoursite.com/page/10/index.html">
<meta property="og:site_name" content="Threadv">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Threadv">
  
    <link rel="alternate" href="/atom.xml" title="Threadv" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/css/style.css">
</head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Threadv</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Suche"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main">
  
    <article id="post-sort/希尔排序(Shell Sort)" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/09/11/sort/希尔排序(Shell Sort)/" class="article-date">
  <time datetime="2018-09-11T11:39:43.000Z" itemprop="datePublished">2018-09-11</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/09/11/sort/希尔排序(Shell Sort)/">希尔排序(Shell Sort)</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p><a href="http://www.runoob.com/w3cnote/sort-algorithm-summary.html" target="_blank" rel="noopener">http://www.runoob.com/w3cnote/sort-algorithm-summary.html</a></p>
<p>动图 ：<a href="https://www.cnblogs.com/onepixel/articles/7674659.html" target="_blank" rel="noopener">https://www.cnblogs.com/onepixel/articles/7674659.html</a></p>
<table>
<thead>
<tr>
<th>排序算法</th>
<th>时间复杂度</th>
</tr>
</thead>
<tbody>
<tr>
<td>冒泡排序</td>
<td>O(n2)</td>
</tr>
<tr>
<td>选择排序</td>
<td>O(n2)</td>
</tr>
<tr>
<td>插入排序</td>
<td>O(n2)</td>
</tr>
<tr>
<td>希尔排序</td>
<td>O(n1.5)</td>
</tr>
<tr>
<td>快速排序</td>
<td>O(N*logN)</td>
</tr>
<tr>
<td>归并排序</td>
<td>O(N*logN)</td>
</tr>
<tr>
<td>堆排序</td>
<td>O(N*logN)</td>
</tr>
<tr>
<td>基数排序</td>
<td>O(d(n+r))</td>
</tr>
</tbody>
</table>
<h5 id="希尔排序-Shell-Sort"><a href="#希尔排序-Shell-Sort" class="headerlink" title="希尔排序(Shell Sort)"></a>希尔排序(Shell Sort)</h5><ul>
<li>前言：</li>
</ul>
<p>数据序列1： 13-17-20-42-28 利用插入排序，13-17-20-28-42. Number of swap:1;</p>
<p>数据序列2： 13-17-20-42-14 利用插入排序，13-14-17-20-42. Number of swap:3;</p>
<p>如果数据序列基本有序，使用插入排序会更加高效。</p>
<ul>
<li>基本思想：<br>在要排序的一组数中，根据某一增量分为若干子序列，并对子序列分别进行插入排序。<br>然后逐渐将增量减小,并重复上述过程。直至增量为1,此时数据序列基本有序,最后进行插入排序。</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">shell_sort</span><span class="params">(<span class="keyword">int</span> array[],<span class="keyword">int</span> lenth)</span></span>&#123;</span><br><span class="line"></span><br><span class="line">   <span class="keyword">int</span> temp = <span class="number">0</span>;</span><br><span class="line">   <span class="keyword">int</span> incre = lenth;</span><br><span class="line"></span><br><span class="line">   <span class="keyword">while</span>(<span class="keyword">true</span>)&#123;</span><br><span class="line">       incre = incre/<span class="number">2</span>;</span><br><span class="line"></span><br><span class="line">       <span class="keyword">for</span>(<span class="keyword">int</span> k = <span class="number">0</span>;k&lt;incre;k++)&#123;    <span class="comment">//根据增量分为若干子序列</span></span><br><span class="line"></span><br><span class="line">           <span class="keyword">for</span>(<span class="keyword">int</span> i=k+incre;i&lt;lenth;i+=incre)&#123;</span><br><span class="line"></span><br><span class="line">               <span class="keyword">for</span>(<span class="keyword">int</span> j=i;j&gt;k;j-=incre)&#123;</span><br><span class="line">                   <span class="keyword">if</span>(array[j]&lt;array[j-incre])&#123;</span><br><span class="line">                       temp = array[j-incre];</span><br><span class="line">                       array[j-incre] = array[j];</span><br><span class="line">                       array[j] = temp;</span><br><span class="line">                   &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                       <span class="keyword">break</span>;</span><br><span class="line">                   &#125;</span><br><span class="line">               &#125;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line"></span><br><span class="line">       <span class="keyword">if</span>(incre == <span class="number">1</span>)&#123;</span><br><span class="line">           <span class="keyword">break</span>;</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/09/11/sort/希尔排序(Shell Sort)/" data-id="ckcc5iwpc008qq4uwxsg9x21h" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Sort/">Sort</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-sort/冒泡排序(BubbleSort)" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/09/05/sort/冒泡排序(BubbleSort)/" class="article-date">
  <time datetime="2018-09-05T04:39:46.000Z" itemprop="datePublished">2018-09-05</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/09/05/sort/冒泡排序(BubbleSort)/">冒泡排序(BubbleSort)</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p><a href="http://www.runoob.com/w3cnote/sort-algorithm-summary.html" target="_blank" rel="noopener">http://www.runoob.com/w3cnote/sort-algorithm-summary.html</a></p>
<p>动图 ：<a href="https://www.cnblogs.com/onepixel/articles/7674659.html" target="_blank" rel="noopener">https://www.cnblogs.com/onepixel/articles/7674659.html</a></p>
<table>
<thead>
<tr>
<th>排序算法</th>
<th>时间复杂度</th>
</tr>
</thead>
<tbody>
<tr>
<td>冒泡排序</td>
<td>O(n2)</td>
</tr>
<tr>
<td>选择排序</td>
<td>O(n2)</td>
</tr>
<tr>
<td>插入排序</td>
<td>O(n2)</td>
</tr>
<tr>
<td>希尔排序</td>
<td>O(n1.5)</td>
</tr>
<tr>
<td>快速排序</td>
<td>O(N*logN)</td>
</tr>
<tr>
<td>归并排序</td>
<td>O(N*logN)</td>
</tr>
<tr>
<td>堆排序</td>
<td>O(N*logN)</td>
</tr>
<tr>
<td>基数排序</td>
<td>O(d(n+r))</td>
</tr>
</tbody>
</table>
<h5 id="冒泡排序-BubbleSort"><a href="#冒泡排序-BubbleSort" class="headerlink" title="冒泡排序(BubbleSort)"></a>冒泡排序(BubbleSort)</h5><ul>
<li>基本思想：两个数比较大小，较大的数下沉，较小的数冒起来。</li>
<li>过程：</li>
</ul>
<ol>
<li>比较相邻的两个数据，如果第二个数小，就交换位置。</li>
<li>从后向前两两比较，一直到比较最前两个数据。最终最小数被交换到起始的位置，这样第一个最小数的位置就排好了。</li>
<li>继续重复上述过程，依次将第2.3…n-1个最小数排好位置。</li>
</ol>
<ul>
<li>平均时间复杂度：O(n2)</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">BubbleSort</span><span class="params">(<span class="keyword">int</span> [] arr)</span></span>&#123;</span><br><span class="line"></span><br><span class="line">     <span class="keyword">int</span> temp;<span class="comment">//临时变量</span></span><br><span class="line">     <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;arr.length-<span class="number">1</span>; i++)&#123;   <span class="comment">//表示趟数，一共arr.length-1次。</span></span><br><span class="line">         <span class="keyword">for</span>(<span class="keyword">int</span> j=arr.length-<span class="number">1</span>; j&gt;i; j--)&#123;</span><br><span class="line"></span><br><span class="line">             <span class="keyword">if</span>(arr[j] &lt; arr[j-<span class="number">1</span>])&#123;</span><br><span class="line">                 temp = arr[j];</span><br><span class="line">                 arr[j] = arr[j-<span class="number">1</span>];</span><br><span class="line">                 arr[j-<span class="number">1</span>] = temp;</span><br><span class="line">             &#125;</span><br><span class="line">         &#125;</span><br><span class="line">     &#125;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>优化：</li>
</ul>
<ol>
<li><p>针对问题：<br>数据的顺序排好之后，冒泡算法仍然会继续进行下一轮的比较，直到arr.length-1次，后面的比较没有意义的。</p>
</li>
<li><p>方案：<br>设置标志位flag，如果发生了交换flag设置为true；如果没有交换就设置为false。<br>这样当一轮比较结束后如果flag仍为false，即：这一轮没有发生交换，说明数据的顺序已经排好，没有必要继续进行下去。</p>
</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">BubbleSort1</span><span class="params">(<span class="keyword">int</span> [] arr)</span></span>&#123;</span><br><span class="line"></span><br><span class="line">   <span class="keyword">int</span> temp;<span class="comment">//临时变量</span></span><br><span class="line">   <span class="keyword">boolean</span> flag;<span class="comment">//是否交换的标志</span></span><br><span class="line">   <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;arr.length-<span class="number">1</span>; i++)&#123;   <span class="comment">//表示趟数，一共arr.length-1次。</span></span><br><span class="line"></span><br><span class="line">       flag = <span class="keyword">false</span>;</span><br><span class="line">       <span class="keyword">for</span>(<span class="keyword">int</span> j=arr.length-<span class="number">1</span>; j&gt;i; j--)&#123;</span><br><span class="line"></span><br><span class="line">           <span class="keyword">if</span>(arr[j] &lt; arr[j-<span class="number">1</span>])&#123;</span><br><span class="line">               temp = arr[j];</span><br><span class="line">               arr[j] = arr[j-<span class="number">1</span>];</span><br><span class="line">               arr[j-<span class="number">1</span>] = temp;</span><br><span class="line">               flag = <span class="keyword">true</span>;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="keyword">if</span>(!flag) <span class="keyword">break</span>;</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/09/05/sort/冒泡排序(BubbleSort)/" data-id="ckcc5iwp8008iq4uwy7g1nl4l" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Sort/">Sort</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-sort/选择排序(SelctionSort)" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/09/05/sort/选择排序(SelctionSort)/" class="article-date">
  <time datetime="2018-09-05T04:39:46.000Z" itemprop="datePublished">2018-09-05</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/09/05/sort/选择排序(SelctionSort)/">选择排序(SelctionSort)</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p><a href="http://www.runoob.com/w3cnote/sort-algorithm-summary.html" target="_blank" rel="noopener">http://www.runoob.com/w3cnote/sort-algorithm-summary.html</a></p>
<p>动图 ：<a href="https://www.cnblogs.com/onepixel/articles/7674659.html" target="_blank" rel="noopener">https://www.cnblogs.com/onepixel/articles/7674659.html</a></p>
<table>
<thead>
<tr>
<th>排序算法</th>
<th>时间复杂度</th>
</tr>
</thead>
<tbody>
<tr>
<td>冒泡排序</td>
<td>O(n2)</td>
</tr>
<tr>
<td>选择排序</td>
<td>O(n2)</td>
</tr>
<tr>
<td>插入排序</td>
<td>O(n2)</td>
</tr>
<tr>
<td>希尔排序</td>
<td>O(n1.5)</td>
</tr>
<tr>
<td>快速排序</td>
<td>O(N*logN)</td>
</tr>
<tr>
<td>归并排序</td>
<td>O(N*logN)</td>
</tr>
<tr>
<td>堆排序</td>
<td>O(N*logN)</td>
</tr>
<tr>
<td>基数排序</td>
<td>O(d(n+r))</td>
</tr>
</tbody>
</table>
<h5 id="选择排序-SelctionSort"><a href="#选择排序-SelctionSort" class="headerlink" title="选择排序(SelctionSort)"></a>选择排序(SelctionSort)</h5><ul>
<li><p>基本思想：<br>在长度为N的无序数组中，第一次遍历n-1个数，找到最小的数值与第一个元素交换；<br>第二次遍历n-2个数，找到最小的数值与第二个元素交换；<br>。。。<br>第n-1次遍历，找到最小的数值与第n-1个元素交换，排序完成。</p>
</li>
<li><p>平均时间复杂度：O(n2)</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">select_sort</span><span class="params">(<span class="keyword">int</span> array[],<span class="keyword">int</span> lenth)</span></span>&#123;</span><br><span class="line"></span><br><span class="line">   <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;lenth-<span class="number">1</span>;i++)&#123;</span><br><span class="line"></span><br><span class="line">       <span class="keyword">int</span> minIndex = i;</span><br><span class="line">       <span class="keyword">for</span>(<span class="keyword">int</span> j=i+<span class="number">1</span>;j&lt;lenth;j++)&#123;</span><br><span class="line">          <span class="keyword">if</span>(array[j]&lt;array[minIndex])&#123;</span><br><span class="line">              minIndex = j;</span><br><span class="line">          &#125;</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="keyword">if</span>(minIndex != i)&#123;</span><br><span class="line">           <span class="keyword">int</span> temp = array[i];</span><br><span class="line">           array[i] = array[minIndex];</span><br><span class="line">           array[minIndex] = temp;</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/09/05/sort/选择排序(SelctionSort)/" data-id="ckcc5iwpj0094q4uwlntv5dmy" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Sort/">Sort</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-内存泄漏" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/07/29/内存泄漏/" class="article-date">
  <time datetime="2018-07-29T13:26:41.000Z" itemprop="datePublished">2018-07-29</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/07/29/内存泄漏/">内存泄漏</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h4 id="内存泄漏"><a href="#内存泄漏" class="headerlink" title="内存泄漏"></a>内存泄漏</h4><p>1.全局集合<br>在大的应用程序中有某种全局的数据储存库是很常见的，例如，一个JNDI树或一个会话表。在这些情况下，必须注意管理储存库的大小。必须有某种机制使得从储存库中移除不再需要的数据。<br>这可能有多种方法，但是最常见的一种是周期性运行的某种清除任务，该任务将验证储存库中的数据，并移除任何不再需要的数据。<br>另一种管理储存库的方法是使用反向链接（referrer）计数，然后集合负责统计集合中每个入口的反向链接的数目。这要求反向链接告诉集合何时会退出入口。当反向链接数目为零时，该元素就可以从集合中移除了。</p>
<p>2.缓存</p>
<p>缓存是一种数据结构，用于快速查找已经执行的操作结果。因此，如果一个操作执行起来很慢，对于常用的输入数据就可以将操作的结果缓存，并在下次调用该操作时使用缓存的数据。<br>缓存通常都是以动态方式实现的，其中新的结果是在执行时添加到缓存中的。典型的算法如下：</p>
<p>（1）检查结果是否在缓存中，如果在，就返回结果。<br>（2）如果结果不在缓存中，就进行计算。<br>（3）将计算出来的结果添加到缓存中，以便以后对该操作的调用可以使用。</p>
<p>该算法的问题（或者说是潜在的内存泄漏）出在最后一步。如果调用该操作时有相当多的不同的输入，就将有相当多的结果存储在缓存中。很明显，这不是正确的方法。</p>
<p>为了预防这种具有潜在破坏性的设计，程序必须确保对于缓存所使用的内存容量有一个上限。因此，更好的算法如下：</p>
<p>（1）检查结果是否在缓存中，如果在，就返回结果。<br>（2）如果结果不在缓存中，就进行计算。<br>（3）如果缓存所占的空间过大，就移除缓存最久的结果。<br>（4）将计算出来的结果添加到缓存中，以便以后对该操作的调用可以使用。</p>
<p>通过始终移除缓存最久的结果，我们实际上进行了这样的假设：在将来，比起缓存最久的数据，最近输入的数据更有可能用到。这通常是一个不错的假设。</p>
<p>新算法将确保缓存的容量处于预定义的内存范围之内。确切的范围可能很难计算，因为缓存中的对象在不断变化，而且它们的引用包罗万象。为缓存设置正确的大小是一项非常复杂的任务，需要将所使用的内存容量与检索数据的速度加以平衡。</p>
<p>解决这个问题的另一种方法是使用java.lang.ref.SoftReference类跟踪缓存中的对象。如果虚拟机的内存用尽而需要更多的堆，那么这种方法可保证这些引用能够被移除。</p>
<p>3.ClassLoader</p>
<p>Java中ClassLoader结构的使用为内存泄漏提供了许多可乘之机。正是该结构本身的复杂性使ClassLoader在内存泄漏方面存在如此多的问题。ClassLoader的特别之处在于它不仅涉及“常规”的对象引用，还涉及元对象引用，比如字段、方法和类。这意味着只要有对字段、方法、类或ClassLoader对象的引用，ClassLoader就会驻留在JVM中，因为ClassLoader本身可以关联许多类及其静态字段，所以就有许多内存被泄漏。</p>
<ul>
<li>确定内存泄漏位置</li>
</ul>
<p>发生内存泄漏的第一个迹象通常是：在应用程序中出现了OutOfMemoryError。这通常发生在你最不愿意它发生的生产环境中，此时几乎不能进行调试。有可能是因为测试环境运行应用程序的方式与生产系统不完全相同，因而导致泄漏只出现在生产中。在这种情况下，需要使用一些开销较低的工具来监控和查找内存泄漏，还需要能够无须重启系统或修改代码就可以将这些工具连接到正在运行的系统上。可能最重要的是，当进行分析时，需要能够断开工具而保持系统不受干扰。</p>
<p>虽然OutOfMemoryError通常都是内存泄漏的信号，但是也有可能因为应用程序确实正在使用这么多的内存。对于后者，或者必须增加JVM可用的堆的数量，或者对应用程序进行某种更改，使它使用较少的内存。但是，在许多情况下，OutOfMemoryError都是内存泄漏的信号。一种查明方法是不间断地监控GC的活动，确定内存使用量是否随着时间增加，如果确实如此，就可能发生了内存泄漏。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/07/29/内存泄漏/" data-id="ckcc5iwlt003zq4uwqs6jdw69" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/java/">java</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-WXPayUtil" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/07/02/WXPayUtil/" class="article-date">
  <time datetime="2018-07-02T12:09:32.000Z" itemprop="datePublished">2018-07-02</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/07/02/WXPayUtil/">WXPayUtil</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h5 id="WXPayUtil"><a href="#WXPayUtil" class="headerlink" title="WXPayUtil"></a>WXPayUtil</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">package</span> com.ifenghui.storybookapi.util.weixin;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.ifenghui.storybookapi.util.weixin.WXPayConstants.SignType;</span><br><span class="line"><span class="keyword">import</span> org.slf4j.Logger;</span><br><span class="line"><span class="keyword">import</span> org.slf4j.LoggerFactory;</span><br><span class="line"><span class="keyword">import</span> org.w3c.dom.Node;</span><br><span class="line"><span class="keyword">import</span> org.w3c.dom.NodeList;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.crypto.Mac;</span><br><span class="line"><span class="keyword">import</span> javax.crypto.spec.SecretKeySpec;</span><br><span class="line"><span class="keyword">import</span> javax.xml.parsers.DocumentBuilder;</span><br><span class="line"><span class="keyword">import</span> javax.xml.parsers.DocumentBuilderFactory;</span><br><span class="line"><span class="keyword">import</span> javax.xml.transform.OutputKeys;</span><br><span class="line"><span class="keyword">import</span> javax.xml.transform.Transformer;</span><br><span class="line"><span class="keyword">import</span> javax.xml.transform.TransformerFactory;</span><br><span class="line"><span class="keyword">import</span> javax.xml.transform.dom.DOMSource;</span><br><span class="line"><span class="keyword">import</span> javax.xml.transform.stream.StreamResult;</span><br><span class="line"><span class="keyword">import</span> java.io.ByteArrayInputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.InputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.StringWriter;</span><br><span class="line"><span class="keyword">import</span> java.security.MessageDigest;</span><br><span class="line"><span class="keyword">import</span> java.util.*;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">WXPayUtil</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * XML格式字符串转换为Map</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> strXML XML字符串</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> XML数据转换后的Map</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Map&lt;String, String&gt; <span class="title">xmlToMap</span><span class="params">(String strXML)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            Map&lt;String, String&gt; data = <span class="keyword">new</span> HashMap&lt;String, String&gt;();</span><br><span class="line">            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();</span><br><span class="line">            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();</span><br><span class="line">            InputStream stream = <span class="keyword">new</span> ByteArrayInputStream(strXML.getBytes(<span class="string">"UTF-8"</span>));</span><br><span class="line">            org.w3c.dom.Document doc = documentBuilder.parse(stream);</span><br><span class="line">            doc.getDocumentElement().normalize();</span><br><span class="line">            NodeList nodeList = doc.getDocumentElement().getChildNodes();</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> idx = <span class="number">0</span>; idx &lt; nodeList.getLength(); ++idx) &#123;</span><br><span class="line">                Node node = nodeList.item(idx);</span><br><span class="line">                <span class="keyword">if</span> (node.getNodeType() == Node.ELEMENT_NODE) &#123;</span><br><span class="line">                    org.w3c.dom.Element element = (org.w3c.dom.Element) node;</span><br><span class="line">                    data.put(element.getNodeName(), element.getTextContent());</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                stream.close();</span><br><span class="line">            &#125; <span class="keyword">catch</span> (Exception ex) &#123;</span><br><span class="line">                <span class="comment">// do nothing</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> data;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception ex) &#123;</span><br><span class="line">            WXPayUtil.getLogger().warn(<span class="string">"Invalid XML, can not convert to map. Error message: &#123;&#125;. XML content: &#123;&#125;"</span>, ex.getMessage(), strXML);</span><br><span class="line">            <span class="keyword">throw</span> ex;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 将Map转换为XML格式的字符串</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> data Map类型数据</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> XML格式的字符串</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">mapToXml</span><span class="params">(Map&lt;String, String&gt; data)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();</span><br><span class="line">        DocumentBuilder documentBuilder= documentBuilderFactory.newDocumentBuilder();</span><br><span class="line">        org.w3c.dom.Document document = documentBuilder.newDocument();</span><br><span class="line">        org.w3c.dom.Element root = document.createElement(<span class="string">"xml"</span>);</span><br><span class="line">        document.appendChild(root);</span><br><span class="line">        <span class="keyword">for</span> (String key: data.keySet()) &#123;</span><br><span class="line">            String value = data.get(key);</span><br><span class="line">            <span class="keyword">if</span> (value == <span class="keyword">null</span>) &#123;</span><br><span class="line">                value = <span class="string">""</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            value = value.trim();</span><br><span class="line">            org.w3c.dom.Element filed = document.createElement(key);</span><br><span class="line">            filed.appendChild(document.createTextNode(value));</span><br><span class="line">            root.appendChild(filed);</span><br><span class="line">        &#125;</span><br><span class="line">        TransformerFactory tf = TransformerFactory.newInstance();</span><br><span class="line">        Transformer transformer = tf.newTransformer();</span><br><span class="line">        DOMSource source = <span class="keyword">new</span> DOMSource(document);</span><br><span class="line">        transformer.setOutputProperty(OutputKeys.ENCODING, <span class="string">"UTF-8"</span>);</span><br><span class="line">        transformer.setOutputProperty(OutputKeys.INDENT, <span class="string">"yes"</span>);</span><br><span class="line">        StringWriter writer = <span class="keyword">new</span> StringWriter();</span><br><span class="line">        StreamResult result = <span class="keyword">new</span> StreamResult(writer);</span><br><span class="line">        transformer.transform(source, result);</span><br><span class="line">        String output = writer.getBuffer().toString(); <span class="comment">//.replaceAll("\n|\r", "");</span></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            writer.close();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">catch</span> (Exception ex) &#123;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> output;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 生成带有 sign 的 XML 格式字符串</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> data Map类型数据</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> key API密钥</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 含有sign字段的XML</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">generateSignedXml</span><span class="params">(<span class="keyword">final</span> Map&lt;String, String&gt; data, String key)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> generateSignedXml(data, key, SignType.MD5);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 生成带有 sign 的 XML 格式字符串</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> data Map类型数据</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> key API密钥</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> signType 签名类型</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 含有sign字段的XML</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">generateSignedXml</span><span class="params">(<span class="keyword">final</span> Map&lt;String, String&gt; data, String key, SignType signType)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        String sign = generateSignature(data, key, signType);</span><br><span class="line">        data.put(WXPayConstants.FIELD_SIGN, sign);</span><br><span class="line">        <span class="keyword">return</span> mapToXml(data);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 判断签名是否正确</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> xmlStr XML格式数据</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> key API密钥</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 签名是否正确</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">boolean</span> <span class="title">isSignatureValid</span><span class="params">(String xmlStr, String key)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        Map&lt;String, String&gt; data = xmlToMap(xmlStr);</span><br><span class="line">        <span class="keyword">if</span> (!data.containsKey(WXPayConstants.FIELD_SIGN) ) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        String sign = data.get(WXPayConstants.FIELD_SIGN);</span><br><span class="line">        <span class="keyword">return</span> generateSignature(data, key).equals(sign);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 判断签名是否正确，必须包含sign字段，否则返回false。使用MD5签名。</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> data Map类型数据</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> key API密钥</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 签名是否正确</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">boolean</span> <span class="title">isSignatureValid</span><span class="params">(Map&lt;String, String&gt; data, String key)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> isSignatureValid(data, key, SignType.MD5);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 判断签名是否正确，必须包含sign字段，否则返回false。</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> data Map类型数据</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> key API密钥</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> signType 签名方式</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 签名是否正确</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">boolean</span> <span class="title">isSignatureValid</span><span class="params">(Map&lt;String, String&gt; data, String key, SignType signType)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (!data.containsKey(WXPayConstants.FIELD_SIGN) ) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        String sign = data.get(WXPayConstants.FIELD_SIGN);</span><br><span class="line">        <span class="keyword">return</span> generateSignature(data, key, signType).equals(sign);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 生成签名</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> data 待签名数据</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> key API密钥</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 签名</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">generateSignature</span><span class="params">(<span class="keyword">final</span> Map&lt;String, String&gt; data, String key)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> generateSignature(data, key, SignType.MD5);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 生成签名. 注意，若含有sign_type字段，必须和signType参数保持一致。</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> data 待签名数据</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> key API密钥</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> signType 签名方式</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 签名</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">generateSignature</span><span class="params">(<span class="keyword">final</span> Map&lt;String, String&gt; data, String key, SignType signType)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        Set&lt;String&gt; keySet = data.keySet();</span><br><span class="line">        String[] keyArray = keySet.toArray(<span class="keyword">new</span> String[keySet.size()]);</span><br><span class="line">        Arrays.sort(keyArray);</span><br><span class="line">        StringBuilder sb = <span class="keyword">new</span> StringBuilder();</span><br><span class="line">        <span class="keyword">for</span> (String k : keyArray) &#123;</span><br><span class="line">            <span class="keyword">if</span> (k.equals(WXPayConstants.FIELD_SIGN)) &#123;</span><br><span class="line">                <span class="keyword">continue</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (data.get(k).trim().length() &gt; <span class="number">0</span>) <span class="comment">// 参数值为空，则不参与签名</span></span><br><span class="line">                sb.append(k).append(<span class="string">"="</span>).append(data.get(k).trim()).append(<span class="string">"&amp;"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        sb.append(<span class="string">"key="</span>).append(key);</span><br><span class="line">        <span class="keyword">if</span> (SignType.MD5.equals(signType)) &#123;</span><br><span class="line">            <span class="keyword">return</span> MD5(sb.toString()).toUpperCase();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span> <span class="keyword">if</span> (SignType.HMACSHA256.equals(signType)) &#123;</span><br><span class="line">            <span class="keyword">return</span> HMACSHA256(sb.toString(), key);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> Exception(String.format(<span class="string">"Invalid sign_type: %s"</span>, signType));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取随机字符串 Nonce Str</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> String 随机字符串</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">generateNonceStr</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> UUID.randomUUID().toString().replaceAll(<span class="string">"-"</span>, <span class="string">""</span>).substring(<span class="number">0</span>, <span class="number">32</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 生成 MD5</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> data 待处理数据</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> MD5结果</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">MD5</span><span class="params">(String data)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        MessageDigest md = MessageDigest.getInstance(<span class="string">"MD5"</span>);</span><br><span class="line">        <span class="keyword">byte</span>[] array = md.digest(data.getBytes(<span class="string">"UTF-8"</span>));</span><br><span class="line">        StringBuilder sb = <span class="keyword">new</span> StringBuilder();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">byte</span> item : array) &#123;</span><br><span class="line">            sb.append(Integer.toHexString((item &amp; <span class="number">0xFF</span>) | <span class="number">0x100</span>).substring(<span class="number">1</span>, <span class="number">3</span>));</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> sb.toString().toUpperCase();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 生成 HMACSHA256</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> data 待处理数据</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> key 密钥</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 加密结果</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">HMACSHA256</span><span class="params">(String data, String key)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        Mac sha256_HMAC = Mac.getInstance(<span class="string">"HmacSHA256"</span>);</span><br><span class="line">        SecretKeySpec secret_key = <span class="keyword">new</span> SecretKeySpec(key.getBytes(<span class="string">"UTF-8"</span>), <span class="string">"HmacSHA256"</span>);</span><br><span class="line">        sha256_HMAC.init(secret_key);</span><br><span class="line">        <span class="keyword">byte</span>[] array = sha256_HMAC.doFinal(data.getBytes(<span class="string">"UTF-8"</span>));</span><br><span class="line">        StringBuilder sb = <span class="keyword">new</span> StringBuilder();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">byte</span> item : array) &#123;</span><br><span class="line">            sb.append(Integer.toHexString((item &amp; <span class="number">0xFF</span>) | <span class="number">0x100</span>).substring(<span class="number">1</span>, <span class="number">3</span>));</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> sb.toString().toUpperCase();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 日志</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Logger <span class="title">getLogger</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        Logger logger = LoggerFactory.getLogger(<span class="string">"wxpay java sdk"</span>);</span><br><span class="line">        <span class="keyword">return</span> logger;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取当前时间戳，单位秒</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">long</span> <span class="title">getCurrentTimestamp</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> System.currentTimeMillis()/<span class="number">1000</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取当前时间戳，单位毫秒</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">long</span> <span class="title">getCurrentTimestampMs</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> System.currentTimeMillis();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 生成 uuid， 即用来标识一笔单，也用做 nonce_str</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">generateUUID</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> UUID.randomUUID().toString().replaceAll(<span class="string">"-"</span>, <span class="string">""</span>).substring(<span class="number">0</span>, <span class="number">32</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/07/02/WXPayUtil/" data-id="ckcc5iwjh0013q4uw67e9q6gg" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/util/">util</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-SortedMap转化成 xml格式" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/07/02/SortedMap转化成 xml格式/" class="article-date">
  <time datetime="2018-07-02T11:09:35.000Z" itemprop="datePublished">2018-07-02</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/07/02/SortedMap转化成 xml格式/">SortedMap 集合转化成 xml格式</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h5 id="SortedMap-集合转化成-xml格式"><a href="#SortedMap-集合转化成-xml格式" class="headerlink" title="SortedMap 集合转化成 xml格式"></a>SortedMap<object,object> 集合转化成 xml格式</object,object></h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * 将SortedMap&lt;Object,Object&gt; 集合转化成 xml格式</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">getRequestXml</span><span class="params">(SortedMap&lt;Object,Object&gt; parameters)</span></span>&#123;</span><br><span class="line">    StringBuffer sb = <span class="keyword">new</span> StringBuffer();</span><br><span class="line">    sb.append(<span class="string">"&lt;xml&gt;"</span>);</span><br><span class="line">    Set es = parameters.entrySet();</span><br><span class="line">    Iterator it = es.iterator();</span><br><span class="line">    <span class="keyword">while</span>(it.hasNext()) &#123;</span><br><span class="line">        Map.Entry entry = (Map.Entry)it.next();</span><br><span class="line">        String k = (String)entry.getKey();</span><br><span class="line">        String v = (String)entry.getValue();</span><br><span class="line">        <span class="keyword">if</span> (<span class="string">"attach"</span>.equalsIgnoreCase(k)||<span class="string">"body"</span>.equalsIgnoreCase(k)||<span class="string">"sign"</span>.equalsIgnoreCase(k)) &#123;</span><br><span class="line">            sb.append(<span class="string">"&lt;"</span>+k+<span class="string">"&gt;"</span>+<span class="string">"&lt;![CDATA["</span>+v+<span class="string">"]]&gt;&lt;/"</span>+k+<span class="string">"&gt;"</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            sb.append(<span class="string">"&lt;"</span>+k+<span class="string">"&gt;"</span>+v+<span class="string">"&lt;/"</span>+k+<span class="string">"&gt;"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    sb.append(<span class="string">"&lt;/xml&gt;"</span>);</span><br><span class="line">    <span class="keyword">return</span> sb.toString();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/07/02/SortedMap转化成 xml格式/" data-id="ckcc5iwj7000rq4uwlkj730s9" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/util/">util</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-XML格式字符串转换为Map" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/07/02/XML格式字符串转换为Map/" class="article-date">
  <time datetime="2018-07-02T11:09:35.000Z" itemprop="datePublished">2018-07-02</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/07/02/XML格式字符串转换为Map/">XML格式字符串转换为Map</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h5 id="XML格式字符串转换为Map"><a href="#XML格式字符串转换为Map" class="headerlink" title="XML格式字符串转换为Map"></a>XML格式字符串转换为Map</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"> </span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * XML格式字符串转换为Map</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> strXML XML字符串</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> XML数据转换后的Map</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Map&lt;String, String&gt; <span class="title">xmlToMap</span><span class="params">(String strXML)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        Map&lt;String, String&gt; data = <span class="keyword">new</span> HashMap&lt;String, String&gt;();</span><br><span class="line">        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();</span><br><span class="line">        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();</span><br><span class="line">        InputStream stream = <span class="keyword">new</span> ByteArrayInputStream(strXML.getBytes(<span class="string">"UTF-8"</span>));</span><br><span class="line">        org.w3c.dom.Document doc = documentBuilder.parse(stream);</span><br><span class="line">        doc.getDocumentElement().normalize();</span><br><span class="line">        NodeList nodeList = doc.getDocumentElement().getChildNodes();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> idx = <span class="number">0</span>; idx &lt; nodeList.getLength(); ++idx) &#123;</span><br><span class="line">            Node node = nodeList.item(idx);</span><br><span class="line">            <span class="keyword">if</span> (node.getNodeType() == Node.ELEMENT_NODE) &#123;</span><br><span class="line">                org.w3c.dom.Element element = (org.w3c.dom.Element) node;</span><br><span class="line">                data.put(element.getNodeName(), element.getTextContent());</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            stream.close();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception ex) &#123;</span><br><span class="line">            <span class="comment">// do nothing</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> data;</span><br><span class="line">    &#125; <span class="keyword">catch</span> (Exception ex) &#123;</span><br><span class="line">        WXPayUtil.getLogger().warn(<span class="string">"Invalid XML, can not convert to map. </span></span><br><span class="line"><span class="string">        Error message: &#123;&#125;. XML content: &#123;&#125;"</span>, ex.getMessage(), strXML);</span><br><span class="line">        <span class="keyword">throw</span> ex;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/07/02/XML格式字符串转换为Map/" data-id="ckcc5iwjp0018q4uw83x5k2yn" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/util/">util</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-月初和月末" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/06/30/月初和月末/" class="article-date">
  <time datetime="2018-06-30T12:15:19.000Z" itemprop="datePublished">2018-06-30</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/06/30/月初和月末/">月初和月末</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h5 id="月初和月末"><a href="#月初和月末" class="headerlink" title="月初和月末"></a>月初和月末</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">Calendar cale = Calendar.getInstance();</span><br><span class="line">  <span class="comment">// 获取当月第一天和最后一天</span></span><br><span class="line">  SimpleDateFormat format = <span class="keyword">new</span> SimpleDateFormat(<span class="string">"yyyy-MM-dd"</span>)</span><br><span class="line">  String firstday, lastday;</span><br><span class="line">  <span class="comment">// 获取前月的第一天</span></span><br><span class="line">  cale.add(Calendar.MONTH, <span class="number">0</span>);</span><br><span class="line">  cale.set(Calendar.DAY_OF_MONTH, <span class="number">1</span>);</span><br><span class="line">  firstday = format.format(cale.getTime());</span><br><span class="line">  Date firstDay = format.parse(firstday);</span><br><span class="line">  <span class="comment">// 获取前月的最后一天</span></span><br><span class="line">  cale = Calendar.getInstance();</span><br><span class="line">  cale.add(Calendar.MONTH, <span class="number">1</span>);</span><br><span class="line">  cale.set(Calendar.DAY_OF_MONTH, <span class="number">0</span>);</span><br><span class="line">  lastday = format.format(cale.getTime());</span><br><span class="line">  Date lastDay = format.parse(lastday);</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/06/30/月初和月末/" data-id="ckcc5iwm7004mq4uwnfjs00qv" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/util/">util</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-Map转换为XML格式" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/06/30/Map转换为XML格式/" class="article-date">
  <time datetime="2018-06-30T12:10:12.000Z" itemprop="datePublished">2018-06-30</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/06/30/Map转换为XML格式/">Map转换为XML格式</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h5 id="Map转换为XML格式"><a href="#Map转换为XML格式" class="headerlink" title="Map转换为XML格式"></a>Map转换为XML格式</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 将Map转换为XML格式的字符串</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> data Map类型数据</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> XML格式的字符串</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">mapToXml</span><span class="params">(Map&lt;String, String&gt; data)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();</span><br><span class="line">    DocumentBuilder documentBuilder= documentBuilderFactory.newDocumentBuilder();</span><br><span class="line">    org.w3c.dom.Document document = documentBuilder.newDocument();</span><br><span class="line">    org.w3c.dom.Element root = document.createElement(<span class="string">"xml"</span>);</span><br><span class="line">    document.appendChild(root);</span><br><span class="line">    <span class="keyword">for</span> (String key: data.keySet()) &#123;</span><br><span class="line">        String value = data.get(key);</span><br><span class="line">        <span class="keyword">if</span> (value == <span class="keyword">null</span>) &#123;</span><br><span class="line">            value = <span class="string">""</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        value = value.trim();</span><br><span class="line">        org.w3c.dom.Element filed = document.createElement(key);</span><br><span class="line">        filed.appendChild(document.createTextNode(value));</span><br><span class="line">        root.appendChild(filed);</span><br><span class="line">    &#125;</span><br><span class="line">    TransformerFactory tf = TransformerFactory.newInstance();</span><br><span class="line">    Transformer transformer = tf.newTransformer();</span><br><span class="line">    DOMSource source = <span class="keyword">new</span> DOMSource(document);</span><br><span class="line">    transformer.setOutputProperty(OutputKeys.ENCODING, <span class="string">"UTF-8"</span>);</span><br><span class="line">    transformer.setOutputProperty(OutputKeys.INDENT, <span class="string">"yes"</span>);</span><br><span class="line">    StringWriter writer = <span class="keyword">new</span> StringWriter();</span><br><span class="line">    StreamResult result = <span class="keyword">new</span> StreamResult(writer);</span><br><span class="line">    transformer.transform(source, result);</span><br><span class="line">    String output = writer.getBuffer().toString(); <span class="comment">//.replaceAll("\n|\r", "");</span></span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        writer.close();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">catch</span> (Exception ex) &#123;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> output;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/06/30/Map转换为XML格式/" data-id="ckcc5iwj0000kq4uw7t2fes0q" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/util/">util</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-java/java_常见问题" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/06/29/java/java_常见问题/" class="article-date">
  <time datetime="2018-06-29T13:26:41.000Z" itemprop="datePublished">2018-06-29</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/06/29/java/java_常见问题/">问题收集1</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h3 id="基本问题"><a href="#基本问题" class="headerlink" title="基本问题"></a>基本问题</h3><p><strong>例1 ：</strong> 下面给出的java中ClassLoader中的描述，哪些描述是正确的？</p>
<pre><code>A. ClassLoader没有层次关系

B. 所有类中的ClassLoader都是AppClassLoader 

C. 通过Class.forName(String className),能够动态加载一个类

D. 不同的ClassLoader加载同一个Class文件，所得的类是相同的
</code></pre><p><strong>例2 ：</strong> Which characters does JVM use(JVM 用哪种字符表示)？</p>
<pre><code>A. ASCII characters

B. Unicode characters

C. Cp1252

D. UTF-8
</code></pre><p><strong>例3 ：</strong> 程序的输出结果？</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Test</span></span>&#123;</span><br><span class="line">    <span class="keyword">static</span>&#123;</span><br><span class="line">        <span class="keyword">int</span> x = <span class="number">5</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">int</span> x,y;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] arg)</span></span>&#123;</span><br><span class="line">        x--;</span><br><span class="line">        myMethod();</span><br><span class="line">        System.out.println(x + y++ + x);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">myMethod</span><span class="params">()</span></span>&#123;</span><br><span class="line">        y = x++ + ++x;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>例4 ：</strong> Given the following class :(给定下面的类：)？</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ZeroPrint</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String argv[])</span></span>&#123;</span><br><span class="line">        <span class="keyword">int</span> i = <span class="number">0</span>;</span><br><span class="line">        <span class="comment">//Here</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>例5 ：</strong> 下列程序的输出结果是（）。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.＊;</span><br><span class="line"><span class="keyword">public</span>   <span class="class"><span class="keyword">class</span>   <span class="title">Test</span>   </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">public</span>   <span class="keyword">static</span>   <span class="keyword">void</span>   <span class="title">main</span><span class="params">(String[]   args)</span>   </span>&#123;</span><br><span class="line">      <span class="keyword">int</span>   j = <span class="number">0</span>;</span><br><span class="line">      <span class="keyword">for</span>   (<span class="keyword">int</span>   i = <span class="number">0</span>; i ＜ <span class="number">100</span>; i++)</span><br><span class="line">      &#123;</span><br><span class="line">              j = j++;</span><br><span class="line">      &#125;</span><br><span class="line">      System.out.println(j);</span><br><span class="line">      &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<pre><code>A.0

B.99

C.100

D.101    
</code></pre><p><strong>例6 ：</strong> If there are “ int a=5，b=3;”，the values of a and b are <strong> and </strong> after execute” if(！(a== b)&amp;&amp;(a== 1+b++)){};”.（假 如 “int a=5，b=3；”，则 执 行 “if(！(a== b)&amp;&amp;(a==1+b++)){}; “后a和b的值分别为<strong>和</strong>。）</p>
<pre><code>A.5,3

B.0,1

C.0,3

D.5,4    
</code></pre><p><strong>例7 ：</strong> 以下代码的执行结果是多少？<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.＊;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span>  <span class="title">Test3</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> i=<span class="number">0</span>;</span><br><span class="line">        i=i++ + ++i;</span><br><span class="line">        <span class="keyword">int</span> j=<span class="number">0</span>;</span><br><span class="line">        j=++j + j++ + j++ + j++;</span><br><span class="line">        <span class="keyword">int</span> k=<span class="number">0</span>;</span><br><span class="line">        k=k++ + k++ + k++ + ++k;</span><br><span class="line">        <span class="keyword">int</span> h=<span class="number">0</span>;</span><br><span class="line">        h=++h + ++h;</span><br><span class="line">        <span class="keyword">int</span> p1=<span class="number">0</span>,p2=<span class="number">0</span>; <span class="keyword">int</span> q1=<span class="number">0</span>,q2=<span class="number">0</span>;</span><br><span class="line">        q1=++p1;</span><br><span class="line">        q2=p2++;</span><br><span class="line">        System.out.println(<span class="string">"i "</span>+i);</span><br><span class="line">        System.out.println(<span class="string">"j "</span>+j);</span><br><span class="line">        System.out.println(<span class="string">"k "</span>+k);</span><br><span class="line">        System.out.println(<span class="string">"h "</span>+h);</span><br><span class="line">        System.out.println(<span class="string">"p1 "</span>+p1);</span><br><span class="line">        System.out.println(<span class="string">"p2 "</span>+p2);</span><br><span class="line">        System.out.println(<span class="string">"q1 "</span>+q1);</span><br><span class="line">        System.out.println(<span class="string">"q2 "</span>+q2);</span><br><span class="line">      &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><strong>例8 ：</strong> 输出结果？<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> i = <span class="number">012</span>;</span><br><span class="line"><span class="keyword">int</span> j = <span class="number">034</span>;</span><br><span class="line"><span class="keyword">int</span> k = (<span class="keyword">int</span>)<span class="number">056L</span>;</span><br><span class="line"><span class="keyword">int</span> l = <span class="number">078</span>;</span><br></pre></td></tr></table></figure></p>
<pre><code>A. 输出12,34,56

B. int K = (int)056L 编译错误

C. 输出10,28,46

D. int l = 078 编译错误
</code></pre><p>​<br><strong>例9 ：</strong> 程序错误的——？</p>
<pre><code>A. short s = 1; s = s+1;;

B. short s = 1; s+=1;
</code></pre><p>​<br>​<br><strong>例10 ：</strong> Which declaration for the main() method in a stand-alone program are NOT valid (哪一个main函数声明是不合法)？</p>
<pre><code>A. public static voif main();

B. public static void main(String[] string);

C. public static void main(String[] exp) throws FileNot FoundException;

D. static void main(String[] args);
</code></pre><p>​<br><strong>例11 ：</strong> 输出结果？</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Test</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">        <span class="keyword">boolean</span> b = <span class="keyword">true</span>?<span class="keyword">false</span>:<span class="keyword">true</span> == <span class="keyword">true</span>?<span class="keyword">false</span>:<span class="keyword">true</span>;</span><br><span class="line">        System.out.println(b);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<pre><code>A. true;

B. false;

C. null;

D. 以上都不对;
</code></pre><p><strong>例12 ：</strong> 输出结果？Which of the following lines if placed after the comment //Here will print out is not 0?(哪一个选项替换掉类中的Here不会输出0？)</p>
<pre><code>A. System.out.print(i++);

B. System.out.print(i);

C. System.out.print(i+&apos;0&apos;);

D. System.out.print(i--);
</code></pre><p><strong>例13 ：</strong> Which of the following  will compile correctly？</p>
<pre><code>A. Short myshort = 99S;

B. foloat z = 1.0;

C. int t = &quot;abc&quot;.length();

D.char c = 17c;
</code></pre><p><strong>例14 ：</strong> 输出结果？</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Test</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">        <span class="keyword">int</span> a = <span class="number">5</span>;</span><br><span class="line">        System.out.println(<span class="string">"value is"</span>+ ((a&lt;<span class="number">5</span>)?<span class="number">10.9</span>:<span class="number">9</span>));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<pre><code>A. 编译错误;

B. 10.9;

C. 9;

D. 以上不对;    
</code></pre><p><strong>例15 ：</strong> 输出结果？</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Test</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">        <span class="keyword">char</span> x = <span class="string">'x'</span>;</span><br><span class="line">        <span class="keyword">int</span> i = <span class="number">10</span>;</span><br><span class="line">        System.out.println(<span class="keyword">false</span> ? i : x);</span><br><span class="line">        System.out.println(<span class="keyword">false</span> ? <span class="number">10</span> : x);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<pre><code>A. 120 x;

B. 120 120;

C. x 120;

D. 以上不对;    
</code></pre><p><strong>例16 ：</strong> What does the following program print (输出结果)？</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> calss Test &#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">        <span class="keyword">int</span> m =<span class="number">5</span> ,n=<span class="number">5</span>;</span><br><span class="line">        <span class="keyword">if</span>((m != <span class="number">5</span>) &amp;&amp; (n++ == <span class="number">5</span>))&#123;&#125;</span><br><span class="line">        System.out.println(<span class="string">"a."</span> + n);</span><br><span class="line">        m = n = <span class="number">5</span>;</span><br><span class="line">        <span class="keyword">if</span>((m != <span class="number">5</span>) &amp; (n++ == <span class="number">6</span>))&#123;&#125;</span><br><span class="line">        System.out.println(<span class="string">"b."</span> + n);</span><br><span class="line">        m = n = <span class="number">5</span>;</span><br><span class="line">        <span class="keyword">if</span>((m == <span class="number">5</span>) || (n++ == <span class="number">5</span>))&#123;&#125;</span><br><span class="line">        System.out.println(<span class="string">"c."</span> + n);</span><br><span class="line">        m = n = <span class="number">5</span>;</span><br><span class="line">        <span class="keyword">if</span>((m==<span class="number">5</span>) | (n++ == <span class="number">6</span>))&#123;&#125;</span><br><span class="line">        System.out.println(<span class="string">"d."</span> + n);</span><br><span class="line">        <span class="keyword">int</span> a =<span class="number">1</span>,b = <span class="number">2</span>;</span><br><span class="line">        <span class="keyword">int</span> c = a &amp; b;</span><br><span class="line">        System.out.println(<span class="string">"a&amp;b"</span> + c);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>例17 ：</strong> 输出结果？</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Test</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">        <span class="keyword">int</span> num = <span class="number">32</span>;</span><br><span class="line">        System.out.println(num &gt;&gt; <span class="number">32</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<pre><code>A. 32;

B. 16;

C. 1;

D. 0;     
</code></pre><hr>
<h4 id="Exception"><a href="#Exception" class="headerlink" title="Exception"></a>Exception</h4><p><strong>例1 ：</strong> Which of the following is NOT true regarding to RuntimeException (关于运行时异常不正确的)？</p>
<pre><code>A. RuntimeException is the superclass of those exceptions that must be thrown during the normal operation of the Java VirtulMachine(运行时异常是一个超类，当Java虚拟机正常时一定抛出);

B. A method is not required t odeclare in its throws clause any  any subclasses of RuntimeExcption that might be thrown during the execution of the method but caught(运行时异常可以不去捕捉);

C. An RuntimeException is a subcalss of Throwable that indicates serious problems that a reasonable application should not try to catch(运行时异常是一个子类，当出现严重运行问题时也不会抛出);

D. NullPointerExcption is one kind of RuntimeException(空异常是一种运行时异常);    
</code></pre><p><strong>例2 ：</strong> 定义了如下类和测试方法，请问测试时期待要捕获下面那个选项的异常？</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyException</span> <span class="keyword">extends</span> <span class="title">Exceeption</span></span>&#123;</span><br><span class="line">    MyException()&#123;&#125;</span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">A</span></span>&#123;</span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">format</span><span class="params">(String str)</span> <span class="keyword">throws</span> MyException </span>&#123;</span><br><span class="line">            <span class="keyword">int</span> i = Integer.valueof(str);</span><br><span class="line">            <span class="keyword">return</span> i;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testTester</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">new</span> A().format(<span class="string">"1"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<pre><code>A. Exception;

B. MyException;

C. MyException和NumberFormatException;

D. RuntimeException;    
</code></pre><p><strong>例3 ：</strong> try{}里有一个return语句，那么紧跟在这个try后面的finally{}里的code会不会被执行，什么时候被执行那个，在return前还是后？</p>
<pre><code>A. 在return前执行;

B. 在return后执行;

C. 不会执行;

D. 抛出异常;    
</code></pre><p>​<br><strong>例4 ：</strong> 下面四段Java程序中哪些不能被编译通过？</p>
<p>程序1：</p>
<pre><code>import java.io.＊;
public class Test {
public static void main(String[] args) {
      try {
            test();
      } catch (Exception ex) {
            ex.printStackTrace();
      }
}
static void test() {
      try {
            throw new Exception(&quot;test&quot;);
      } catch (Exception ex) {
            ex.printStackTrace();
      }
 }
}
</code></pre><p>程序2：</p>
<pre><code>import java.io.＊;
public class Test {
public static void main(String[] args) {
      try {
            test();
      }
      catch (Exception ex) {
            exprintStackTrace();
      }
 }
 static void test() {
      throw new Exception(&quot;test&quot;);
 }
}
</code></pre><p>程序3：</p>
<pre><code>import java.io.＊;
public class Test {
public static void main(String[] args) {
      try {
            test();
      } catch (Exception ex) {
            ex.printStackTrace();
      }
 }
 static void test() throws Exception {
       throw new Exception(&quot;test&quot;);
 }
}
</code></pre><p>程序4：</p>
<pre><code>import java.io.＊;
public class Test {
public static void main(String[] args) {
      try {
            test();
      } catch (IoException ex) {
            ex.printStackTrace();
      }
 }
 static void test() throws Exception {
 }
}
</code></pre><p>-<br>        A.程序1和程序2</p>
<pre><code>C.程序1和程序3

B.程序3和程序4

D.程序2和程序4
</code></pre><p>​<br><strong>例5 ：</strong> 输出结果？</p>
<pre><code>public class Test{
    public static void main(String[] args){
        try{
            System.out.println(&quot;condition 1&quot;);
        }catch(ArrayIndexoutofBoundsException){
            System.out.println(&quot;condition 2&quot;);
        }catch(Exception e){
            System.out.println(&quot;condition 3&quot;);
        }finally{
            System.out.println(&quot;finally&quot;);
        }
        //test();
    }
    static void test(){
        String str = &quot;cc&quot;;
        str.compareTo(&quot;abc&quot;);
    }
}  
</code></pre><hr>
<h4 id="静态变量-amp-私有变量"><a href="#静态变量-amp-私有变量" class="headerlink" title="静态变量&amp;私有变量"></a>静态变量&amp;私有变量</h4><p><strong>例1 ：</strong> 输出结果？</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Value</span> </span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> i = <span class="number">15</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Test</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">        Test t = <span class="keyword">new</span> Test();</span><br><span class="line">        t.first();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">first</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">int</span> i = <span class="number">5</span>;</span><br><span class="line">        Value v = <span class="keyword">new</span> Value();</span><br><span class="line">        v.i = <span class="number">25</span>;</span><br><span class="line">        second(v,i);</span><br><span class="line">        System.out.println(v.i);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">second</span><span class="params">(Value v,<span class="keyword">int</span> i)</span></span>&#123;</span><br><span class="line">        i = <span class="number">0</span>;</span><br><span class="line">        v.i = <span class="number">20</span>;</span><br><span class="line">        Value val = <span class="keyword">new</span> Value();</span><br><span class="line">        v = val;</span><br><span class="line">        System.out.println(v.i +<span class="string">" "</span>+ i);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>例2 ：</strong> 静态变量的创建？</p>
<pre><code>A. 一旦一个静态变量被分配，它就不允许改变;

B. 一个静态变量在一个方法中创建，它在被调用时候值保持不变;

C. 在任意多个类的实例中，一个静态变量的实例只存在一个;

D. 一个静态的标识符只能被应用于primitive value;    
</code></pre><p><strong>例3 ：</strong> 编译运行下列代码会出现什么情况？</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Sandys</span></span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> court;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">        Sandys s = <span class="keyword">new</span> Sandys(<span class="number">99</span>);</span><br><span class="line">        System.out.println(s.court);</span><br><span class="line">    &#125;</span><br><span class="line">    Sandys(<span class="keyword">int</span> vallcount)&#123;</span><br><span class="line">        court = ballcount;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<pre><code>A. 编译错误，court变量定义的是私有变量;

B. 编译错误，当System.out.println方法被调用是s没有被初始化;

C. 编译和运行时没有输出结果;

D. 编译和运行时输出的结果是99;    
</code></pre><p><strong>例4 ：</strong> 编译运行下列代码会出现什么情况？</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Pvf</span></span>&#123;</span><br><span class="line">   <span class="keyword">static</span> <span class="keyword">boolean</span> Paddy;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">        System.out.println(Paddy);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<pre><code>A. 编译时错误;

B. 编译时通过并输出结果false;

C. 编译通过并输出结果true;

D. 编译通过并输出结果null;    
</code></pre><p><strong>例5 ：</strong> 用命令行参数Java Sytch Jones Diggle 编译和运行程序出现的结果？</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Sytch</span></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> x = <span class="number">2000</span>;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"Ms "</span>+ args[<span class="number">1</span>] + <span class="string">"please pay $"</span> +x);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<pre><code>A. 编译通过并输出Ms Diggle please pay $2000 结果;

B. 编译时错误;

C. 编译通过并输出Ms Jones please pay $2000 ;

D. 编译通过但是运行是错误;    
</code></pre><h3 id="输入-输出流"><a href="#输入-输出流" class="headerlink" title="输入/输出流"></a>输入/输出流</h3><p><strong>例1 ：</strong> 假设异常处理已经被创建，下列哪个是创建RandomAccessFile类实例？</p>
<pre><code>A. RandomAccessFile raf = new RandomAccessFile(&quot;myfile.txt&quot;,&quot;rw&quot;);

B. RandomAccessFile raf = new RandomAccessFile(new Data ImputStr-eam());

C. RandomAccessFile raf = new RandomAccessFile(&quot;myfile.txt&quot;);

D. RandomAccessFile raf = new RandomAccessFile(new File(&quot;myfile.txt&quot;));    
</code></pre><p><strong>例2 ：</strong> 需要读一个比较大的文本文件，这个文件里有很多字节的数据，最适合读这类文件的是？</p>
<pre><code>A. new FileImputStream(&quot;file.name&quot;);

B. new InputStreamReader(new FileInputStream(&quot;file.name&quot;));

C. new BufferReader(new ImputStreamReader(new FileInputStream(&quot;file.name&quot;)));

D. new RandomAccessFile raf = new RandomAccessFile(&quot;myfile.txt&quot;,&quot;+rw&quot;);    
</code></pre><p><strong>例3 ：</strong> 描述字符串写入文件？</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.io.*;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Test</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">        <span class="keyword">try</span>&#123;</span><br><span class="line">            FileOutPutStream out = <span class="keyword">new</span> FileOutStream(<span class="string">"filename.txt"</span>);</span><br><span class="line">            out.wtite(<span class="string">"字符串写入文件"</span>.getBytes());</span><br><span class="line">            out.close;</span><br><span class="line">        &#125;<span class="keyword">catch</span>(IOexception e)&#123;</span><br><span class="line">            </span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>例4 ：</strong> 键入俩个整数，输出平方值和立方值？</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">InputData</span></span>&#123;</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">private</span> String s = <span class="string">""</span>;</span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">public</span> <span class="keyword">void</span>  <span class="title">input</span><span class="params">()</span></span>&#123;</span><br><span class="line">        BufferdReader bu = <span class="keyword">new</span> BufferedReader(<span class="keyword">new</span> InputStreamReader(System.in));</span><br><span class="line">        <span class="keyword">try</span>&#123;</span><br><span class="line">            s = bu.readLine();</span><br><span class="line">        &#125;<span class="keyword">catch</span>(IoException e)&#123;&#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="function"><span class="keyword">static</span> <span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getInt</span><span class="params">()</span></span>&#123;</span><br><span class="line">            input();</span><br><span class="line">            <span class="keyword">return</span> Integer.parseInt(s);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">Result</span></span>&#123;</span><br><span class="line">        <span class="function"><span class="keyword">void</span> <span class="title">print</span> <span class="params">(<span class="keyword">int</span> d)</span></span>&#123;</span><br><span class="line">            System.out.println(d + <span class="string">"的平方："</span>+d*d);</span><br><span class="line">            System.out.println(d + <span class="string">"的立方："</span>+d*d*d);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> class <span class="title">printResult</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">            Result result = <span class="keyword">new</span> Result();</span><br><span class="line">            System.out.println(<span class="string">"请输入一个整数："</span>);</span><br><span class="line">            <span class="keyword">int</span> a = InputData.getInt();</span><br><span class="line">            result.print(a);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>例5 ：</strong> 判断某个整数是否为2的阶次方数？ </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">InputData</span></span>&#123;</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">private</span> String s = <span class="string">""</span>;</span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title">input</span><span class="params">()</span></span>&#123;</span><br><span class="line">        BufferedReader bu =<span class="keyword">new</span> BufferedReader(<span class="keyword">new</span> InputStreamReader(System.in));</span><br><span class="line">        <span class="keyword">try</span>&#123;</span><br><span class="line">            s = bu.readLine();</span><br><span class="line">        &#125;<span class="keyword">catch</span> (IOException e)&#123;&#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getInt</span><span class="params">()</span></span>&#123;</span><br><span class="line">        input();</span><br><span class="line">        <span class="keyword">return</span> Integer.parseInt(s);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Result</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">print</span><span class="params">(<span class="keyword">int</span> d)</span></span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(((d-<span class="number">1</span>)&amp;(d)) == <span class="number">0</span> &amp;&amp; (d!=<span class="number">0</span>))&#123;</span><br><span class="line">            System.out.println(<span class="string">"是2的阶次"</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            System.out.println(<span class="string">"不是2 的阶次"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ttest1</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        Result result = <span class="keyword">new</span> Result();</span><br><span class="line">        System.out.println(<span class="string">"请输入一个整数："</span>);</span><br><span class="line">        <span class="keyword">int</span>  a  = InputData.getInt();</span><br><span class="line">        result.print(a);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="递归"><a href="#递归" class="headerlink" title="递归"></a>递归</h3><p><strong>例1 ：</strong> 数组元素的全部组合？</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">     <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        String arr[] = <span class="keyword">new</span> String[]&#123;<span class="string">"1"</span>, <span class="string">"2"</span>, <span class="string">"3"</span>, <span class="string">"4"</span>&#125;;</span><br><span class="line">        listAll(Arrays.asList(arr), <span class="string">""</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">listAll</span><span class="params">(List candidate, String prefix)</span> </span>&#123;</span><br><span class="line"><span class="comment">//        if (candidate.isEmpty()) &#123;</span></span><br><span class="line">            System.out.println(prefix);</span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; candidate.size(); i++) &#123;</span><br><span class="line">            List temp = <span class="keyword">new</span> LinkedList(candidate);</span><br><span class="line">            listAll(temp,prefix + temp.remove(i));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p><strong>例2 ：</strong> 菲波那切数列？</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">  <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> k =<span class="number">0</span>;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    Scanner sc = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line">    <span class="keyword">long</span> a = sc.nextLong();</span><br><span class="line">    System.out.println(test(a));</span><br><span class="line">    System.out.println(<span class="string">"共调用了"</span>+k+<span class="string">"次"</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">long</span> <span class="title">test</span><span class="params">(<span class="keyword">long</span> m)</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(m==<span class="number">0</span> || m==<span class="number">1</span>)&#123;</span><br><span class="line">        k++;</span><br><span class="line">        <span class="keyword">return</span> m;</span><br><span class="line">    &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> test(m-<span class="number">1</span>)+test(m-<span class="number">2</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">Recursion</span><span class="params">(<span class="keyword">int</span> n)</span></span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span>(n==<span class="number">1</span>)&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span>(n==<span class="number">2</span>)&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> Recursion(n-<span class="number">1</span>)+Recursion(n-<span class="number">2</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>例3 ：</strong> 一个字符串中可能包含a～z中的多个字符，如有重复，如String data=”aavzcadfdsfsdhshgWasdfasdfdddaaa”，求出现次数最多的那个字母及次数，如有多个重复的则都求出。</p>
<pre><code>（1）引入TreeSet：通过集合快速找到所有出现的字符串。
（2）引入ArrayList：为了快速排序，再通过StringBuffer生成排序后的字符串。
（3）通过String api中的基本方法indexOf lastIndexOf来计算TreeSet中每个字符串的最大值。
（4）如果出现相同的，则把相同的都记录在一个列表中。
（5）记录第一个出现次数最多的字符（为了计算多个字符串相同情况）。
（6）计算最大字符串列表中哪些才是真正出现次数最多的。
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.Collections;</span><br><span class="line"><span class="keyword">import</span> java.util.Iterator;</span><br><span class="line"><span class="keyword">import</span> java.util.TreeSet;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Test</span> </span>&#123;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">      <span class="comment">// ToDo Auto-generated method stub</span></span><br><span class="line">      String input = <span class="string">"aavzcadfdsfsdhshgwasdfasdfdddaaa"</span>;</span><br><span class="line">      <span class="keyword">new</span> Test().doString(input);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">doString</span><span class="params">(String input)</span> </span>&#123;</span><br><span class="line">      <span class="keyword">char</span>[] chars = input.toCharArray();</span><br><span class="line">      ArrayList lists = <span class="keyword">new</span> ArrayList();</span><br><span class="line">      TreeSet set = <span class="keyword">new</span> TreeSet();</span><br><span class="line">      <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i ＜ chars.length; i++) &#123;</span><br><span class="line">          lists.add(String.valueof(chars[i]));</span><br><span class="line">          set.add(String.valueof(chars[i]));</span><br><span class="line">      &#125;</span><br><span class="line">      System.out.println(set);</span><br><span class="line">      Collections.sort(lists);</span><br><span class="line">      System.out.println(lists);</span><br><span class="line">      StringBuffer sb = <span class="keyword">new</span> StringBuffer();</span><br><span class="line">      <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i ＜ lists.size(); i++) &#123;</span><br><span class="line">          sb.append(lists.get(i));</span><br><span class="line">      &#125;</span><br><span class="line">      input = sb.toString();</span><br><span class="line">      System.out.println(input);</span><br><span class="line">      <span class="keyword">int</span> max = <span class="number">0</span>;</span><br><span class="line">      String maxString = <span class="string">""</span>;</span><br><span class="line">      ArrayList maxlist = <span class="keyword">new</span> ArrayList();</span><br><span class="line">      Iterator its = set.iterator();</span><br><span class="line">      <span class="keyword">while</span> (its.hasNext()) &#123;</span><br><span class="line">          String os = (String) its.next();</span><br><span class="line">          <span class="keyword">int</span> begin = input.indexof(os);</span><br><span class="line">          <span class="keyword">int</span> end = input.lastIndexof(os);</span><br><span class="line">          <span class="keyword">int</span> value = end - begin + <span class="number">1</span>;</span><br><span class="line">          <span class="keyword">if</span> (value ＞ max) &#123;</span><br><span class="line">              max = value;</span><br><span class="line">              maxString = os;</span><br><span class="line">              maxlist.add(os);</span><br><span class="line">          &#125; <span class="keyword">else</span> <span class="keyword">if</span> (value == max) &#123;</span><br><span class="line">              maxlist.add(os);</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">int</span> index = <span class="number">0</span>;</span><br><span class="line">      <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i ＜ maxlist.size(); i++) &#123;</span><br><span class="line">          <span class="keyword">if</span> (maxlist.get(i).equals(maxString)) &#123;</span><br><span class="line">              index = i;</span><br><span class="line">              <span class="keyword">break</span>;</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;</span><br><span class="line">      System.out.print(<span class="string">"max data"</span>);</span><br><span class="line">      <span class="keyword">for</span> (<span class="keyword">int</span> i = index; i ＜ maxlist.size(); i++) &#123;</span><br><span class="line">          System.out.print(maxlist.get(i) + <span class="string">" "</span>);</span><br><span class="line">      &#125;</span><br><span class="line">      System.out.println();</span><br><span class="line">      System.out.println(<span class="string">"max"</span> + max);</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>例4 ：</strong> 利用1、2、2、3、4这5个数字，用Java写一个main函数，打印出所有不同的排列，如12234、21234等，要求打印出来的不能有重复。</p>
<pre><code>对于任意一个串利用递归进行排列时，循环串中的每个字符到第一个字符进行递归。如果串中字符出现重复，则重复的字符只可以利用递归算法一次，即只要与前面相同的字符循环到第一个字符时，不调用递归就可以避免重复。
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.＊;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">test123</span> </span>&#123;</span><br><span class="line"><span class="keyword">static</span> <span class="keyword">int</span> count=<span class="number">0</span>;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] arg)</span> </span>&#123;</span><br><span class="line"><span class="comment">//Scanner r=new Scanner(System.in);</span></span><br><span class="line">        String s=<span class="string">"1223"</span>;</span><br><span class="line">        String s2=<span class="string">"1232"</span>;</span><br><span class="line">        <span class="keyword">int</span> t = <span class="number">1</span>&amp;<span class="number">0</span>;</span><br><span class="line">        System.out.println(t);</span><br><span class="line">        /＊</span><br><span class="line">        <span class="keyword">int</span> cc=s2.indexof(s2.charAt(<span class="number">1</span>));</span><br><span class="line">        <span class="keyword">int</span> cc2=s2.indexof(s2.charAt(<span class="number">2</span>));</span><br><span class="line">        System.out.println(s2.charAt(<span class="number">0</span>));</span><br><span class="line">        System.out.println(s2.charAt(<span class="number">1</span>));</span><br><span class="line">        System.out.println(s2.charAt(<span class="number">2</span>));</span><br><span class="line">        System.out.println(s2.charAt(<span class="number">3</span>));</span><br><span class="line">        System.out.println(s2.indexof(s2.charAt(<span class="number">0</span>)));</span><br><span class="line">        System.out.println(s2.indexof(s2.charAt(<span class="number">1</span>)));</span><br><span class="line">        System.out.println(s2.indexof(s2.charAt(<span class="number">2</span>)));</span><br><span class="line">        System.out.println(s2.indexof(s2.charAt(<span class="number">3</span>)));</span><br><span class="line">        ＊/</span><br><span class="line">        <span class="keyword">int</span> index[]=<span class="keyword">new</span> <span class="keyword">int</span>[s.length()];</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i＜s2.length(); i++)               <span class="comment">//该循环将所有的字符第一次出现的位置记录在数组</span></span><br><span class="line">                                                        <span class="comment">//index中</span></span><br><span class="line">        &#123;index[i]=s2.indexof(s2.charAt(i));</span><br><span class="line">        <span class="comment">//System.out.println(s2.indexof(s2.charAt(i)));</span></span><br><span class="line">        &#125;</span><br><span class="line">        Pailie(s,<span class="string">""</span>);</span><br><span class="line">        <span class="comment">//System.out.println("Total:"+count);</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Pailie</span><span class="params">(String s,String p)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(s.length()＜<span class="number">1</span>) &#123;</span><br><span class="line">        <span class="comment">//System.out.println(p);                        //字符串长度小于1，换行</span></span><br><span class="line">        count++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">int</span> index[]=<span class="keyword">new</span> <span class="keyword">int</span>[s.length()];</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i＜s.length(); i++)                <span class="comment">//该循环将所有的字符第一次出现的位置记录在数组index中</span></span><br><span class="line">        &#123;index[i]=s.indexof(s.charAt(i));</span><br><span class="line">        <span class="comment">//System.out.println(s.charAt(i));</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i＜s.length(); i++) &#123;</span><br><span class="line">        <span class="keyword">if</span>(i==index[i])                                 <span class="comment">//只有当循环数与第一次记录数相等时才递归，保证相同字符</span></span><br><span class="line">                                                        <span class="comment">//中的第一个调用</span></span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">//System.out.println("ppp"+index[i]);</span></span><br><span class="line">           Pailie(s.substring(<span class="number">1</span>),p+s.substring(<span class="number">0</span>,<span class="number">1</span>));  <span class="comment">//递归，打印其他字符</span></span><br><span class="line">        &#125;</span><br><span class="line">            s=s.substring(<span class="number">1</span>)+s.substring(<span class="number">0</span>,<span class="number">1</span>);          <span class="comment">//循环移位</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​<br>​    </p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/06/29/java/java_常见问题/" data-id="ckcc5iwog0071q4uw66k2362s" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/java/">java</a></li></ul>

    </footer>
  </div>
  
</article>


  


  <nav id="page-nav">
    
    <a class="extend prev" rel="prev" href="/page/9/">&laquo; zurück</a><a class="page-number" href="/">1</a><span class="space">&hellip;</span><a class="page-number" href="/page/8/">8</a><a class="page-number" href="/page/9/">9</a><span class="page-number current">10</span><a class="page-number" href="/page/11/">11</a><a class="page-number" href="/page/12/">12</a><span class="space">&hellip;</span><a class="page-number" href="/page/15/">15</a><a class="extend next" rel="next" href="/page/11/">weiter &raquo;</a>
  </nav>

</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Design-Patterns/">Design Patterns</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Git/">Git</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Html/">Html</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Http/">Http</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/JavaScript/">JavaScript</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Sort/">Sort</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/algorithm/">algorithm</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/ali-weixin-pay/">ali/weixin pay</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/docker/">docker</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/elasticsearch/">elasticsearch</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/go/">go</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hexo/">hexo</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/idea/">idea</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/java/">java</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/jvm/">jvm</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/linux/">linux</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/logstash/">logstash</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mysql/">mysql</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/php/">php</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/pytest/">pytest</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/python/">python</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/springboot/">springboot</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/util/">util</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/vue/">vue</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/wechat/">wechat</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/我的世界/">我的世界</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/消息队列/">消息队列</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/线程/">线程</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/Design-Patterns/" style="font-size: 16.36px;">Design Patterns</a> <a href="/tags/Git/" style="font-size: 10px;">Git</a> <a href="/tags/Html/" style="font-size: 10px;">Html</a> <a href="/tags/Http/" style="font-size: 12.73px;">Http</a> <a href="/tags/JavaScript/" style="font-size: 15.45px;">JavaScript</a> <a href="/tags/Sort/" style="font-size: 18.18px;">Sort</a> <a href="/tags/algorithm/" style="font-size: 11.82px;">algorithm</a> <a href="/tags/ali-weixin-pay/" style="font-size: 13.64px;">ali/weixin pay</a> <a href="/tags/docker/" style="font-size: 17.27px;">docker</a> <a href="/tags/elasticsearch/" style="font-size: 10px;">elasticsearch</a> <a href="/tags/go/" style="font-size: 11.82px;">go</a> <a href="/tags/hexo/" style="font-size: 10px;">hexo</a> <a href="/tags/idea/" style="font-size: 10px;">idea</a> <a href="/tags/java/" style="font-size: 20px;">java</a> <a href="/tags/jvm/" style="font-size: 11.82px;">jvm</a> <a href="/tags/linux/" style="font-size: 18.18px;">linux</a> <a href="/tags/logstash/" style="font-size: 12.73px;">logstash</a> <a href="/tags/mysql/" style="font-size: 14.55px;">mysql</a> <a href="/tags/php/" style="font-size: 11.82px;">php</a> <a href="/tags/pytest/" style="font-size: 10px;">pytest</a> <a href="/tags/python/" style="font-size: 13.64px;">python</a> <a href="/tags/springboot/" style="font-size: 17.27px;">springboot</a> <a href="/tags/util/" style="font-size: 19.09px;">util</a> <a href="/tags/vue/" style="font-size: 10px;">vue</a> <a href="/tags/wechat/" style="font-size: 10.91px;">wechat</a> <a href="/tags/我的世界/" style="font-size: 10px;">我的世界</a> <a href="/tags/消息队列/" style="font-size: 10.91px;">消息队列</a> <a href="/tags/线程/" style="font-size: 11.82px;">线程</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archiv</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/05/">May 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/04/">April 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/03/">March 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/01/">January 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/10/">October 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/08/">August 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/06/">June 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/04/">April 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/03/">March 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/02/">February 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/01/">January 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/12/">December 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/11/">November 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/10/">October 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/09/">September 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/07/">July 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/06/">June 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/05/">May 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/04/">April 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/03/">March 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/02/">February 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/01/">January 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/12/">December 2017</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">letzter Beitrag</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2020/05/17/linux/linux_shell/">linux—shell</a>
          </li>
        
          <li>
            <a href="/2020/05/16/linux/linux_awk_sed/">linux-awk\sed 示例</a>
          </li>
        
          <li>
            <a href="/2020/04/11/linux/linux_bash/">linux—bash</a>
          </li>
        
          <li>
            <a href="/2020/03/29/hello-world/">Hello World</a>
          </li>
        
          <li>
            <a href="/2020/03/16/go基础/">go基础知识</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 vchen<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/script.js"></script>



  </div>
</body>
</html>